package com.yuncheng.spcyApi.selectXzxkApi.api;

import com.yuncheng.spcyApi.alog.annotation.Log;

import com.yuncheng.spcyApi.busService.MyJbxxService;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.selectXzxkApi.service.PutZwxtService;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiJscjjgParamVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiJscjsqParamVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiJsywbmthParamVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiJszgjgParamVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 政务服务系统 - 对接政务服务接口功能
 */
@Api(tags = "政务服务系统-对接审评中心服务接口功能")
@RestController
@RequestMapping("/api/spzx-spcy/djzwfw")
public class PutZwxtApi {

    private static final Logger log = LoggerFactory.getLogger(PutZwxtApi.class);

    @Resource
    private PutZwxtService putZwxtService;

    @Resource
    @Lazy
    private MyJbxxService myJbxxService;

    @Resource
    private VerifyTokenApi verifyTokenApi;

    private static final boolean VERIIFY_TOKEN = true;

    @Log(title = "接受企业提交补充信息及材料结果接口")
    @ApiOperation(value = "接受企业提交补充信息及材料结果接口")
    @PostMapping(value = "jsbcxxjcljg")
    public HttpResult jsbcxxjcljg(@RequestBody XzspJkApiJszgjgParamVo jszgjgVo){

        if (VERIIFY_TOKEN) {
            if (StringUtils.isBlank(jszgjgVo.getAccess_token())) {
                throw new BusinessException("TOKEN为空！");
            }

            String username = null;

            try {
                username = verifyTokenApi.validAccessTokenWithScopeAndReturnUsername(jszgjgVo.getAccess_token());
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("无效TOKEN");
            }

            if (null == username) {
                throw new BusinessException("无效TOKEN");
            }
        }

        putZwxtService.jsQyTjbqbzJg(jszgjgVo);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "接受企业提交补充整改结果接口")
    @ApiOperation(value = "接受企业提交补充整改结果接口")
    @PostMapping(value = "jszgjg")
    public HttpResult jszgjg(@RequestBody XzspJkApiJszgjgParamVo jszgjgVo){

        if (VERIIFY_TOKEN) {
            if (StringUtils.isBlank(jszgjgVo.getAccess_token())) {
                throw new BusinessException("TOKEN为空！");
            }
            String username = null;

            try {
                username = verifyTokenApi.validAccessTokenWithScopeAndReturnUsername(jszgjgVo.getAccess_token());
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("无效TOKEN");
            }

            if (null == username) {
                throw new BusinessException("无效TOKEN");
            }
        }

        putZwxtService.jsQytjbczgJg(jszgjgVo);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "接受撤件申请信息接口")
    @ApiOperation(value = "接受撤件申请信息接口")
    @PostMapping(value = "jscjsqxx")
    public HttpResult jscjsqxx(@RequestBody XzspJkApiJscjsqParamVo jscjsqVo){

        if (VERIIFY_TOKEN) {
            if (StringUtils.isBlank(jscjsqVo.getAccess_token())) {
                throw new BusinessException("TOKEN为空！");
            }

            String username = null;

            try {
                username = verifyTokenApi.validAccessTokenWithScopeAndReturnUsername(jscjsqVo.getAccess_token());
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("无效TOKEN");
            }

            if (null == username) {
                throw new BusinessException("无效TOKEN");
            }
        }

        putZwxtService.jscjsqxx(jscjsqVo);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "接受撤件结果信息接口")
    @ApiOperation(value = "接受撤件结果信息接口")
    @PostMapping(value = "jscjjg")
    public HttpResult jscjjg(@RequestBody XzspJkApiJscjjgParamVo jscjjgVo){

        if (VERIIFY_TOKEN) {
            if (StringUtils.isBlank(jscjjgVo.getAccess_token())) {
                throw new BusinessException("TOKEN为空！");
            }
            String username = null;

            try {
                username = verifyTokenApi.validAccessTokenWithScopeAndReturnUsername(jscjjgVo.getAccess_token());
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("无效TOKEN");
            }

            if (null == username) {
                throw new BusinessException("无效TOKEN");
            }
        }

        putZwxtService.jscjjgxx(jscjjgVo);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "接受业务部门退回接口")
    @ApiOperation(value = "接受业务部门退回接口")
    @PostMapping(value = "jsywbmth")
    public HttpResult jsywbmth(@RequestBody XzspJkApiJsywbmthParamVo jsywbmthVo){

        if (VERIIFY_TOKEN) {
            if (StringUtils.isBlank(jsywbmthVo.getAccess_token())) {
                throw new BusinessException("TOKEN为空！");
            }

            String username = null;

            try {
                username = verifyTokenApi.validAccessTokenWithScopeAndReturnUsername(jsywbmthVo.getAccess_token());
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("无效TOKEN");
            }

            if (null == username) {
                throw new BusinessException("无效TOKEN");
            }
        }

        putZwxtService.zwfwxtYwbmReturnJob(jsywbmthVo);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "行政审批-系统调用-我的待办数量")
    @ApiOperation(value = "行政审批-系统调用-我的待办数量")
    @GetMapping(value = "mySpcyFlowNum")
    public HttpResult<?> mySpcyFlowNum(@RequestParam(value = "loginName",defaultValue = "")String loginName,
                                       @RequestParam(value = "sessionToken",defaultValue = "")String sessionToken){

        if (StringUtils.isNotBlank(sessionToken)) {
            putZwxtService.verifyTokenInfo(sessionToken);
        }

        if (StringUtils.isBlank(loginName)){
            throw new BusinessException("登录账号为空！");
        }

        Map resultMap = new HashMap();

        String allnum = "allnum";
        long num = 0;

        if (StringUtils.isNotBlank(loginName)) {
            YcSysUser nowUser = myJbxxService.getNowUserByLoginName(loginName);
            if (nowUser !=  null){
                num = myJbxxService.mySpcyFlowNum(nowUser.getId());
            }
        }

        resultMap.put(allnum, num);
        return HttpResult.ok(resultMap);
    }


    @PostMapping("/testToken")
    public HttpResult<?> testToken(@RequestBody XzspJkApiJscjsqParamVo jscjsqVo){
        String username = null;

        try {
            username = verifyTokenApi.validAccessTokenWithScopeAndReturnUsername(jscjsqVo.getAccess_token());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("无效TOKEN");
        }

        if (null == username) {
//            this.errorToken(response);
            throw new BusinessException("无效TOKEN");
        }

//        System.out.println(username);

        return HttpResult.ok("操作成功");
    }

    @GetMapping(value = "jscjsqxx2")
    public HttpResult jscjsqxx2(@RequestParam(value = "test",defaultValue = "")String test){
        return HttpResult.ok(test);
    }

    /** 测试 */
    @PostMapping(value = "jsbcxxjcljgTest")
    public HttpResult jsbcxxjcljgTest(@RequestBody XzspJkApiJszgjgParamVo jszgjgVo){
        log.info("接受企业提交补充信息及材料结果接口");

        putZwxtService.jsQyTjbqbzJgTest(jszgjgVo);

        return HttpResult.ok("操作成功");
    }
}
